//
//  HEEduSelectionCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEEduSelectionCell.h"
#import "HEEduSelectionVideoCell.h"

@interface HEEduSelectionCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UILabel *titleLb;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
@property (nonatomic, strong) NSArray <HEVideoListModel *> *showList;

@end
 
@implementation HEEduSelectionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _titleLb = [UILabel new];
    _titleLb.text = @"选集";
    _titleLb.font = DYBlodFont(17);
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_titleLb];
    
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    _layout.estimatedItemSize = CGSizeMake(120, 100);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self.contentView addSubview:_collectionView];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.height.offset(140);
        make.bottom.equalTo(self.contentView).mas_offset(-7);
    }];
    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceHorizontal = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HEEduSelectionVideoCell.class forCellWithReuseIdentifier:KHEEduSelectionVideoCell];
}

- (void)configCellWithModel:(id)model indexPath:(NSIndexPath *)indexPath{
    _delegate = self.proxyVC;
    
    HEEduVideoModel *item = (HEEduVideoModel *)model;
    
    _showList = item.video;
    [_collectionView reloadData];
}
 
- (CGFloat)getHeight {
    [_collectionView reloadData];
    return self.layout.collectionViewContentSize.height;
}

//- (void)getFooterViewHeightBlock:(getViewHeightBlock)block {
//    _heightBlock = block;
//}

//解决ios8上自动布局的问题
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _showList.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEEduSelectionVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: KHEEduSelectionVideoCell forIndexPath:indexPath];
        cell.model = _showList[indexPath.row];
    return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(190, 140);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HEVideoListModel *model = self.showList[indexPath.item];

    if (model.isCurrentPlay) {
        return;
    }
    
    model.isCurrentPlay = YES;
    [self restOther:model];
    [self.collectionView reloadData];
    if ([_delegate respondsToSelector:@selector(playSelectionVideo:)]) {
        [_delegate playSelectionVideo:model];
    }
}

- (void)restOther:(HEVideoListModel *)selectModel {
    for (HEVideoListModel *model in self.showList) {
        if (selectModel.id == model.id) {
            continue;;
        }
        model.isCurrentPlay = NO;
    }
}

@end
